package com.doaction.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

public class UserLoginPanel extends Composite implements ClickHandler {

	private Label emailLabel = new Label("Email");
	private TextBox emailText = new TextBox();
	private Label passwordLabel = new Label("Password");
	private PasswordTextBox passwordText = new PasswordTextBox();
	private Button submitButton = new Button("Login", this);
	private VerticalPanel panel = new VerticalPanel();

	public UserLoginPanel() {
		panel.add(emailLabel);
		panel.add(emailText);
		panel.add(passwordLabel);
		panel.add(passwordText);
		panel.add(submitButton);

		initWidget(panel);
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();
		if (sender == submitButton) {
			UserLoginServiceAsync service = GWT.create(UserLoginService.class);
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable caught) {
					Window.alert("fail");

				}

				@Override
				public void onSuccess(Boolean result) {

					if (result.booleanValue()) {
						Window.alert("logged");
						StackLayoutPanel p = new StackLayoutPanel(Unit.EM);
						p.add(new HTML("this content"), new HTML("this"), 4);
						p.add(new HTML("that content"), new HTML("that"), 4);
						p.add(new HTML("the other content"), new HTML(
								"the other"), 4);
						RootPanel.get().clear();
						RootPanel.get().add(p);
					}
				}

			};

			if (emailText.getText().length() == 0
					|| passwordText.getText().length() == 0) {
				Window.alert("Error");
				return;
			}

			service.userLogin(emailText.getText(), passwordText.getText(),
					callback);
		}
	}

}
